﻿#region

using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using KCMS.Bootstraper;
using KCMS.Controllers;

#endregion

namespace KCMS.UI
{
    public class MvcApplication : HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
           null, // Route name
           "новости", // URL with parameters
           new { controller = "News", action = "ShowALL" },
                // new { index = @"(\d+)" },
            new[] { "KCMS.Controllers" }// Parameter defaults
           );

            routes.MapRoute(
           null, // Route name
           "новости/{code}.html", // URL with parameters
           new { controller = "News", action = "Show", id = UrlParameter.Optional, code = "" },
                // new { index = @"(\d+)" },
            new[] { "KCMS.Controllers" }// Parameter defaults
           );
            /*---------------Магазин-------------------*/

            routes.MapRoute(
                null, // Route name
                "товары/поиск_по_артиклу.html", // URL with parameters
                new { controller = "Product", action = "GetProductByArticle", id = UrlParameter.Optional },
                 new[] { "KCMS.Controllers" }// Parameter defaults
                );
            routes.MapRoute(
                     null, // Route name
                     "товары/{code}_{index}.html", // URL with parameters
                     new { controller = "Product", action = "Show", id = UrlParameter.Optional, code = "" }
                     , new { index = @"(\d+)" },
                      new[] { "KCMS.Controllers" }// Parameter defaults
                     );


            routes.MapRoute(
              null, // Route name
              "товары/{code}.html", // URL with parameters
              new { controller = "ProductCategory", action = "Show", id = UrlParameter.Optional, code = "" },
                // new { index = @"(\d+)" },
               new[] { "KCMS.Controllers" }// Parameter defaults
              );


            routes.MapRoute(
               "Cart", // Route name
               "корзина.html", // URL with parameters
               new { controller = "Cart", action = "Show" },
                new[] { "KCMS.Controllers" }// Parameter defaults
               );

            /*--------------Контакты-----------------*/
            routes.MapRoute(
                "Contact", // Route name
                "контакты.html", // URL with parameters
                new { controller = "Contact", action = "ShowAll" },
                 new[] { "KCMS.Controllers" }// Parameter defaults
                );

            routes.MapRoute(
                null, // Route name
                "{code}.html", // URL with parameters
                new { controller = "Page", action = "Show", id = UrlParameter.Optional, code = "" },
                // new { index = @"(\d+)" },
                 new[] { "KCMS.Controllers" }// Parameter defaults
                );
            routes.MapRoute(
                null, // Route name
                "{category}/{code}.html", // URL with parameters
                new { controller = "Page", action = "ShowWithCategory", id = UrlParameter.Optional, code = "", category = "категории" },
                //, new { index = @"(\d+)" },
                 new[] { "KCMS.Controllers" }// Parameter defaults
                );

            routes.MapRoute(
                null, // Route name
                "товары/{category}/{code}_{index}.html", // URL with parameters
                new { controller = "Product", action = "ShowWithCategory", id = UrlParameter.Optional, code = "", category = "категории" }
                , new { index = @"(\d+)" },
                 new[] { "KCMS.Controllers" }// Parameter defaults
                );

            //routes.MapRoute(
            //    "Page", // Route name
            //    "{*category}", // URL with parameters
            //     new { controller = "Page", action = "Show", id = UrlParameter.Optional },
            //     new[] { "KCMS.Controllers" }// Parameter defaults
            //    );

            /*-------------Deafult---------------------*/
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                new[] { "KCMS.Controllers" }
                );





        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            GlobalRegistry.Initialize();
        }
        protected void Application_EndRequest()
        {
            if (Context.Response.StatusCode == 404)
            {
                Response.Clear();

                var rd = new RouteData();
                rd.DataTokens["area"] = ""; // In case controller is in another area
                rd.Values["controller"] = "Errors";
                rd.Values["action"] = "NotFound";

                IController c = new ErrorsController();
                c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
            }
        }
    }
}